package com.yjd.comm.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.DispatcherServlet;

import com.yjd.comm.base.exception.BaseException;
import com.yjd.comm.base.model.ResultModel;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.comm.vo.Constant.HttpScopeEnum;
import com.yjd.comm.vo.Constant.ResultEnum;

/**
 * @ClassName: DispatcherServletWrap
 * @Description: spring分发器包装类
 * @author ready likun_557@126.com
 * @date 2014-11-3 下午10:54:23
 */
public class DispatcherServletWrap extends DispatcherServlet {

	private static Logger logger = Logger
			.getLogger(DispatcherServletWrap.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 操作异常提示页面
	 */
	private String operateExceptionUrl = "/WEB-INF/jsp/index.jsp";

	/**
	 * 登入页面
	 */
	private String loginUrl = "/WEB-INF/login.jsp";

	/**
	 * 404页面
	 */
	private String page404 = "";

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		operateExceptionUrl = config.getInitParameter("operateExceptionUrl");
		String loginUrlTemp = config.getInitParameter("loginUrl");
		page404 = config.getInitParameter("page404");
		if (StringUtil.isNotEmpty(loginUrlTemp)) {
			this.loginUrl = loginUrlTemp;
		}
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		inService(request, response);
	}

	protected void inService(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpServletRequest processedRequest = request;
		processedRequest = this.checkMultipart(request);
		request = processedRequest != request ? processedRequest : request;
		// 判断是否是ajax请求
		boolean isAjaxRequest = ServletUtil.isAjaxRequest(request);
		try {
			super.service(request, response);
		} catch (Exception e) {
			if (isAjaxRequest) {
				// ajax异常处理
				ResultModel resultModel = ResultModel.createResultModel();
				if (e.getCause() instanceof BaseException) {
					logger.error(e.getMessage());
					BaseException oe = (BaseException) e.getCause();
					resultModel.setCode(oe.getCode());
					resultModel.setResult(oe.getResult());
					resultModel.setReferer(oe.getReferer());
					resultModel.setMessage(oe.getMessage());
				} else {
					logger.error(e.getMessage(), e);
					resultModel.setResult(ResultEnum.ERROR);
					resultModel
							.setMessage(FrameUtil.getResource("sysEx.00001"));
				}
				try {
					ServletUtil.outputJsonMessage(request, response,
							resultModel.toMap());
				} catch (Exception e1) {
					throw new RuntimeException(
							FrameUtil.getResource("sysEx.00001"));
				}
			} else {
				// 非ajax异常处理
				BaseException e1 = null;
				if (e instanceof BaseException) {
					e1 = (BaseException) e;
				} else if (e.getCause() instanceof BaseException) {
					e1 = (BaseException) e.getCause();
				}
				if (e1 != null) {
					logger.error(e.getMessage());
					ServletUtil.putValueToScope(
							request,
							response,
							"result",
							ResultModel.createResultModel1(e1.getResult(),
									null, e1.getReferer(), e1.getMessage())
									.toMap(), HttpScopeEnum.REQUEST);

					// 跳转到操作错误页面(BaseException异常有对应的视图界面)
					request.getRequestDispatcher(operateExceptionUrl).forward(
							request, response);
				} else {
					logger.error(e.getMessage(), e);
					ServletUtil.putValueToScope(request, response,
							ServletUtil.OPERATEEXCEPTIONINFO_REQUEST,
							FrameUtil.getResource("sysEx.00001"),
							HttpScopeEnum.REQUEST);
					request.getRequestDispatcher(operateExceptionUrl).forward(
							request, response);
				}
			}

		}
	}

}
